﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using CoiffeurProject.Screens;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using CoiffeurProject.Utils;
using System.Windows.Media;

namespace CoiffeurProject.UserControls
{
    internal class CustTextBox
    {
        Grid main_container;
        Image focused, notFocused;
        TextBox textBox;
        String initial_text;
        SolidColorBrush black, gray;

        internal CustTextBox(String initial_text, Thickness margin, int row, int col)
        {
            this.initial_text = initial_text;

            #region Initialize Grid

            main_container = new Grid()
            {
                Margin = margin,
                Height = 46,
                Width = 495,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            Grid.SetRow(main_container, row);
            Grid.SetColumn(main_container, col);

            #endregion

            #region Initialize Controls

            focused = new Image()
            {
                Height = 46,
                Width = 495,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Stretch = Stretch.Fill,
                Source = new BitmapImage(new Uri(ImagesPath.ActiveTB)),
                Visibility = Visibility.Hidden
            };

            main_container.Children.Add(focused);

            notFocused = new Image()
            {
                Height = 46,
                Width = 495,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Stretch = Stretch.Fill,
                Source = new BitmapImage(new Uri(ImagesPath.UnactiveTB))
            };

            main_container.Children.Add(notFocused);

            gray = new SolidColorBrush(Colors.Gray);
            black = new SolidColorBrush(Colors.Black);

            textBox = new TextBox()
            {
                Text = initial_text,
                Height = 46,
                Width = 475,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
                BorderThickness = new Thickness(0),
                Background = new SolidColorBrush(Colors.Transparent),
                Margin = new Thickness(10, 0, 0, 0),
                FontSize = 20,
                FontFamily = new FontFamily("Segoe UI"),
                Foreground = gray,
                FontStyle = FontStyles.Italic
            };

            textBox.GotFocus += new RoutedEventHandler(textBox_GotFocus);
            textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus);

            main_container.Children.Add(textBox);

            #endregion

            Modified = false;
        }

        internal CustTextBox(String initial_text, int width, int height, string img_uri, string active_img_uri, 
            Thickness margin, int row, int col)
        {
            this.initial_text = initial_text;

            #region Initialize Grid

            main_container = new Grid()
            {
                Margin = margin,
                Height = height, // 46
                Width = width, // 495
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };

            Grid.SetRow(main_container, row);
            Grid.SetColumn(main_container, col);

            #endregion

            #region Initialize Controls

            focused = new Image()
            {
                Height = height,
                Width = width,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Stretch = Stretch.Fill,
                Source = new BitmapImage(new Uri(active_img_uri)),
                Visibility = Visibility.Hidden
            };

            main_container.Children.Add(focused);

            notFocused = new Image()
            {
                Height = height,
                Width = width,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Stretch = Stretch.Fill,
                Source = new BitmapImage(new Uri(img_uri))
            };

            main_container.Children.Add(notFocused);

            gray = new SolidColorBrush(Colors.Gray);
            black = new SolidColorBrush(Colors.Black);

            textBox = new TextBox()
            {
                Text = initial_text,
                Height = height,
                Width = width - 20,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                VerticalContentAlignment = VerticalAlignment.Top,
                BorderThickness = new Thickness(0),
                Background = new SolidColorBrush(Colors.Transparent),
                Margin = new Thickness(10, 10, 0, 0),
                FontSize = 20,
                FontFamily = new FontFamily("Segoe UI"),
                Foreground = gray,
                FontStyle = FontStyles.Italic
            };

            textBox.GotFocus += new RoutedEventHandler(textBox_GotFocus);
            textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus);

            main_container.Children.Add(textBox);

            #endregion

            Modified = false;
        }

        public void AddTextChangedHandler(Action<object, TextChangedEventArgs> action)
        {
            textBox.TextChanged += new TextChangedEventHandler(action);
            textBox.TextChanged += new TextChangedEventHandler(delegate(object s, TextChangedEventArgs e) { if (textBox.Text != "") Modified = true; else Modified = false; });
        }

        public void ChangeText(string text)
        {
            textBox.Text = text;
            textBox.FontStyle = FontStyles.Normal;
            textBox.Foreground = black;
            Modified = true;
        }

        void textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (textBox.Text == "")
            {
                textBox.Text = initial_text;
                textBox.FontStyle = FontStyles.Italic;
                textBox.Foreground = gray;
                Modified = false;
            }

            focused.Visibility = Visibility.Hidden;
            notFocused.Visibility = Visibility.Visible;
        }

        void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (textBox.Text == initial_text)
            {
                textBox.Text = "";
                textBox.FontStyle = FontStyles.Normal;
                textBox.Foreground = black;
            }

            focused.Visibility = Visibility.Visible;
            notFocused.Visibility = Visibility.Hidden;
        }

        public Boolean Modified { get; set; }

        public Grid MainContainer
        {
            get { return main_container; }
            set { main_container = value; }
        }

        public TextBox TextBox
        {
            get { return textBox; }
        }

        public String Text
        {
            get { return textBox.Text; }
        }
    }
}
